const fs = require('fs-extra')
const path = require('path')
const mdx = require('@mdx-js/mdx')
const del = require('del')


function GetFolderNameArr(path, bool) {
    let components = []
    const files = fs.readdirSync(path)
    //console.log(files, "========files==========");
    files.forEach(function (item, index) {
        let stat = fs.lstatSync(path + item)
        if (stat.isDirectory() === bool) {
            components.push(item)
        }
    })
    return components;
}

function GetFolderPathArr(path, bool) {
    let components = []
    const files = fs.readdirSync(path)
    //console.log(files, "========files==========");
    files.forEach(function (item, index) {
        let stat = fs.lstatSync(path + item)
        if (stat.isDirectory() === bool) {
            components.push(path + item + '/')
        }
    })
    return components;
}



var entryPath = './pages/'
let pageSubFolder = GetFolderNameArr(entryPath, true)
console.log(pageSubFolder, "========pageSubFolder==========");

pageSubFolder.forEach(item => {

    //fs.mkdirSync(entryPath+"./第二个目录");


    //取文件
    var demoSubJsx = GetFolderNameArr(entryPath + item + '/demo/', false);

    console.log(demoSubJsx, "========demoSubJsx==========");
    fs.removeSync(entryPath + item + '/demo_create/')

    var all_import_str_arr = [];
    var all_import_key_arr = [];

    demoSubJsx.forEach(jsx => {


        fs.mkdirpSync(entryPath + item + '/demo_create/' + jsx.split('.')[0]);



        var fromJsx = entryPath + item + '/demo/' + jsx;
        var toFolder = entryPath + item + '/demo_create/' + jsx.split('.')[0];
        var toJsx = toFolder + '/' + 'show.jsx';

        console.log(fromJsx, "====原jsx====");

        console.log(toJsx, "====到jsx====");


        fs.copySync(fromJsx, toJsx)
        fs.copySync('./pages/template.jsx', toFolder + '/index.jsx')

        const mdxJsxText = fs.readFileSync(fromJsx, 'utf8')
        var jsxCodeShow = ["```jsx", `${mdxJsxText}`, "```"].join('\r\n')
        fs.writeFile(toFolder + '/code.mdx', jsxCodeShow, function () { });

        var myDemoName = jsx.split('.')[0];
        var import_str = `import ${myDemoName} from './demo_create/${myDemoName}/index.jsx';`
        all_import_key_arr.push(myDemoName);
        all_import_str_arr.push(import_str);

    })


    console.log(all_import_str_arr, "=========all_import_str_arr=========");

    var inner_jsx = all_import_key_arr.map((item) => {
        return `<${item} />`
    })




    var demo_all_jsx_template = `
import React from 'react';

${all_import_str_arr.join('\n')}

import './index.scss'

export default () => <React.Fragment>
    ${inner_jsx.join('\n\t')}
</React.Fragment>`

    fs.writeFile(entryPath + item + '/demo_all.jsx', demo_all_jsx_template, function () { });


})



// let componentsDemoFolder = GetFolderNameArr('./pages')


// const mdxJsxText = fs.readFileSync('./show.mdx', 'utf8')
// // const jsx = mdx.sync(mdxText)


// var jsxTemplate = ["```jsx", `${mdxJsxText}`, "```"].join('\r\n')


// fs.writeFile(path.join(__dirname, './code.mdx'), jsxTemplate, function () { });
